package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Exceptions.CheckItemException;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @outhor
 * @date 2020/1/10 14:28
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void CheckItemAdd(CheckItem checkItem) {
        checkItemMapper.CheckItemAdd(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckItem> checkItemList = checkItemMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), checkItemList);
    }

    @Override
    public void CheckItemUpdata(CheckItem checkItem) {
        checkItemMapper.CheckItemUpdata(checkItem);
    }

    @Override
    public void CheckItemDelete(Integer id) {
        Integer count = checkItemMapper.findCountById(id);
        if (count > 0) {
            throw new CheckItemException("检查组中存在引用,不能删除");
        } else {
            checkItemMapper.CheckItemDelete(id);
        }
    }

    @Override
    public CheckItem findById(Integer id) {

        return checkItemMapper.findById(id);
    }

    @Override
    public List<CheckItem> checkItemFindAll() {
        return checkItemMapper.checkItemFindAll();
    }
}
